<?php

//print_r($_POST);
 
// username and password sent from form 
//

//print_r($_POST);
//echo "<br>";
//print_r($_FILES["imageFile"]);
//return;
//error_log($post, 0);
				$type=$_POST['type'];
				
				if($type=='item'){
					if(!isset($_POST['scan_code'])||$_POST['scan_code']==''){
					header("location:../index.php?page=manage_items");
						return;
					}
					$filename=$_POST['scan_code'];
					
					$path="../images/item/";
					$statu='scan_code';
					$page='alter_item';
				}elseif($type=='user'){
					if(!isset($_POST['u_id'])||$_POST['u_id']==''){
						header("location:../index.php?page=manage_users");
						return;
					}
					$filename=$_POST['u_id'];
					$path="../images/user/";
					$statu='id';
					$page='current_user';
				}
				
		
			echo "size is :".($_FILES["imageFile"]["size"]/1024)."kb<br>";
		echo "type is :".$_FILES["imageFile"]["type"]."<br>";
			
		$allowedExts = array("jpg", "jpeg","JPG","JPEG");
		$extension = end(explode(".", $_FILES["imageFile"]["name"]));
					echo "ext is ".$extension."<br>";
		
		if($extension==''){
			header("location:../index.php?$statu=$filename&page=$page&stat=error&error=missing");
		}elseif (($_FILES["imageFile"]["size"]/1024) > 200){
			
			header("location:../index.php?$statu=$filename&page=$page&stat=error&error=size");
		}elseif( !in_array($extension, $allowedExts)){
			//return;
			header("location:../index.php?$statu=$filename&page=$page&stat=error&error=format");
		}elseif ($_FILES["file"]["error"] > 0){
		 
			header("location:../index.php?$statu=$filename&page=$page&stat=error&error=unknow");
		}else{
		    echo "Upload: " . $_FILES["imageFile"]["name"] . "<br />";
		    echo "Type: " . $_FILES["imageFile"]["type"] . "<br />";
		    echo "Size: " . ($_FILES["imageFile"]["size"] / 1024) . " Kb<br />";
		    echo "Temp file: " . $_FILES["imageFile"]["tmp_name"] . "<br />";
		
		    if (file_exists($path . $filename.'.jpg')){
		      echo $filename . " already exists. ";
			  fclose($path . $filename.'.jpg');
		       if(!unlink($path . $filename.'.jpg')){
		     	header("location:../index.php?$statu=$filename&page=$page&stat=error&error=unlink");
		       }
		    }
		    
			if(move_uploaded_file($_FILES["imageFile"]["tmp_name"], $path . $filename.'.jpg')){
			      	header("location:../index.php?$statu=$filename&page=$page&stat=success");
			}else{
			      	header("location:../index.php?$statu=$filename&page=$page&stat=error&error=unknown");
			}
		      
		}
	   
		
		  
		  
  
return;

?>